package com.ruihang.generalibrary.ui.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.IProgressShowing;
import com.ruihang.generalibrary.utils.GeneraLifeObser;

import org.xutils.x;

/**
 * Created by Administrator on 2017/6/21.
 */

public class BaseGeneraFragment extends Fragment implements IProgressShowing {
    public static final String EX_ID = "ex_id";
    public static final String EX_TITLE = "ex_title";
    public static final String EX_TYPE = "ex_type";
    public static final String EX_DATA = "ex_data";
    public static final String EX_HTML = "ex_html";
    public static final String EX_URL = "ex_url";
    public static final String EX_TYPE_NEWS = "ex_type_news";
    public static final String EX_NEWS_ITEM = "ex_news_item";
    public static final String EX_COLLECT_ITEM = "ex_collect_item";
    public static final String EX_COMMENT_TYPE = "ex_comment_type";

    protected IProgressShowing parentProgressShowing;
    private ProgressDialog dialog;
    /**
     * 父fragment是否处于显示状态
     */
    protected boolean isParentShowing = true;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_ATTACH);
        if (getParentFragment() != null && getParentFragment() instanceof IProgressShowing) {
            parentProgressShowing = (IProgressShowing) getParentFragment();
        } else if (context instanceof IProgressShowing) {
            parentProgressShowing = (IProgressShowing) context;
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_CREATED);
    }

    @Override
    public void onStart() {
        super.onStart();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_START);
    }

    @Override
    public void onResume() {
        super.onResume();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_RESUME);
    }

    @Override
    public void onPause() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_PAUSE);
        super.onPause();
    }

    @Override
    public void onStop() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_STOP);
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DESTORY_V);
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DESTORY);
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DETACH);
        super.onDetach();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = x.view().inject(this, inflater, container);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_CREATED_V);
        return v;
    }

    @Override
    public boolean showProgress(int level, int pNumber) {
        if (level == LEVEL_ACTIVITY) {
            if (parentProgressShowing != null) {
                parentProgressShowing.showProgress(level, pNumber);
            } else {
                JLog.e("parentProgressShowing is null with level=" + level + " number=" + pNumber);
                showProgress();
            }
        } else if (level == LEVEL_PARENT) {
            if (pNumber <= 0 || parentProgressShowing == null) {
                JLog.e("showing in self with level=" + level + " number=" + pNumber);
                showProgress();
            } else {
                parentProgressShowing.showProgress(level, pNumber - 1);
            }
        } else {
            showProgress();
        }
        return false;
    }

    @Override
    public boolean isProgressShowing(int level, int pNumber) {
        boolean b = parentProgressShowing != null && parentProgressShowing.isProgressShowing(level, pNumber);
        return isProgressShowing() || b;
    }

    @Override
    public boolean closeProgress(int level, int pNumber) {
        if (level == LEVEL_ACTIVITY) {
            if (parentProgressShowing != null) {
                parentProgressShowing.closeProgress(level, pNumber);
            } else {
                JLog.e("parentProgressShowing is null with level=" + level + " number=" + pNumber);
                closeProgress();
            }
        } else if (level == LEVEL_PARENT) {
            if (pNumber <= 0 || parentProgressShowing == null) {
                closeProgress();
            } else {
                parentProgressShowing.closeProgress(level, pNumber - 1);
            }
        } else {
            closeProgress();
        }
        return false;
    }

    public void setParentProgressShowing(IProgressShowing parentProgressShowing) {
        this.parentProgressShowing = parentProgressShowing;
    }

    public String getOwnerName() {
        return this + "";
    }

    protected void showProgress() {
    }

    protected void closeProgress() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    protected void showProgress(String msg) {
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage(msg);
        dialog.show();
    }

    protected boolean isProgressShowing() {
        return false;
    }

    protected void showParentProgress(int pNumber) {
        if (pNumber <= 0 || parentProgressShowing == null) {
            JLog.e("showing in self with level=" + LEVEL_PARENT + " number=" + pNumber);
            showProgress();
        } else {
            parentProgressShowing.showProgress(LEVEL_PARENT, pNumber - 1);
        }
    }

    public void setParentShowing(boolean parentShowing) {
        isParentShowing = parentShowing;
    }

    protected void showParentProgress() {
        showParentProgress(1);
    }

    protected void showActivityProgress() {
        if (parentProgressShowing == null) {
            JLog.e("parentProgressShowing is null with activity level");
            showProgress();
        } else {
            parentProgressShowing.showProgress(LEVEL_ACTIVITY, 0);
        }
    }

    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }

    public void onToHided() {
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    public boolean onBackPressed() {
        return false;
    }

    protected void toast(String str) {
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }

    protected void toast(@StringRes int strRes) {
        Toast.makeText(getContext(), strRes, Toast.LENGTH_SHORT).show();
    }

    public View getScrolledView() {
        return null;
    }

    /**
     * n能否下拉刷新
     *
     * @param e
     * @return
     */
    public boolean canScrollUp(MotionEvent e) {
        return false;
    }

    /**
     * 能否上啦加载
     *
     * @param e
     * @return
     */
    public boolean canScrollDown(MotionEvent e) {
        return false;
    }
}
